package com.cumulus.acceleration;


import java.util.ArrayList;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import android.util.Log;
import com.cumulus.*;
import com.cumulus.MoveCopy.CreateOwnActivity;
import com.cumulus.MoveCopy.ReplyActivity;

public class AccelerationManager implements SensorEventListener {
	private SensorManager sensorManager;

	private static final String Tag = "AccelerationManager";
	ArrayList<AccAng> coord = new ArrayList<AccAng>();
	ArrayList<Angle> angle = new ArrayList<Angle>();
	float[] gravity = new float[3];
	private int index_coord = 0;
	private int index_angle = 0;
	private final int thin = 1;
	
	private float[] mValuesMagnet = new float[3];
	private float[] mValuesAccel = new float[3];
	private float[] mValuesOrientation = new float[3];
	private float[] mRotationMatrix = new float[9];
	private Activity _curActivity;
 
	
	float xA = 0, yA = 0, zA = 0, xC = 0, yC = 0, zC = 0;

	public AccelerationManager(SensorManager sManger, Activity activity) {
		sensorManager = sManger;
		// add listener. The listener will be HelloAndroid (this) class
		sensorManager.registerListener(this,
				sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),
				SensorManager.SENSOR_DELAY_NORMAL);
		sensorManager.registerListener(this, 
				sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE),
				SensorManager.SENSOR_DELAY_NORMAL);
//		sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
//                SensorManager.SENSOR_DELAY_NORMAL);
//        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), 
//                SensorManager.SENSOR_DELAY_NORMAL);

		/*	More sensor speeds (taken from api docs)
		    SENSOR_DELAY_FASTEST get sensor data as fast as possible
		    SENSOR_DELAY_GAME	rate suitable for games
		 	SENSOR_DELAY_NORMAL	rate (default) suitable for screen orientation changes
		*/
		_curActivity = activity;
	}

	public void onAccuracyChanged(Sensor sensor,int accuracy){

	}
	
	
	// stops monitoring the acc/gyroscope movement data and return the data
	public ArrayList<AccAng> stop() {
		sensorManager.unregisterListener(this);
		return coord;
	}
	

	private float distance(AccAng a) {
		float xA = a.xA, yA = a.yA, zA = a.zA, xC = a.xC, yC = a.yC, zC = a.zC;
		float C = (float) 0.3;
		return (C * C * (xA * xA + yA * yA + zA * zA) + (xC * xC + yC * yC + zC * zC));
	}

	public void onSensorChanged(SensorEvent event){

		if(event.sensor.getType()==Sensor.TYPE_LINEAR_ACCELERATION){
	         xC = event.values[0];
	         yC = event.values[1];
	         zC = event.values[2];

	         System.out.println(_curActivity);
	         if(xC > 7 || yC > 7 || zC > 7){
	        	 Log.d(Tag, "Called");
	        	 Log.d("curtitle", _curActivity.getTitle() + "");
	        	 if(_curActivity.getTitle() == "ReplyActivity")
	        		 ((ReplyActivity) _curActivity).playSound();
	        	 else
	        		 ((CreateOwnActivity) _curActivity).playSound();

	         }
	         
	         index_coord++;
		}
//		if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER) {
//			System.arraycopy(event.values, 0, mValuesAccel, 0, 3);
//			Log.d(Tag, "acc is:" + mValuesAccel[0]);
//		}
//		if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
//			System.arraycopy(event.values, 0, mValuesMagnet, 0, 3);
//			Log.d(Tag, "mmagnet is " + mValuesMagnet[0]);
//		}
		if(event.sensor.getType()==Sensor.TYPE_GYROSCOPE){
			xA=event.values[0];
			yA=event.values[1];
			zA=event.values[2];
			
			index_angle++;
		}
		
//	   SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet);
//	   Log.d(Tag, "Angle is:" + SensorManager.getInclination(mRotationMatrix));
//		
		if(index_coord == index_angle && index_coord % thin == 0) {
			coord.add(new AccAng(xA, yA, zA, xC, yC, zC));
			int ind = coord.size() - 1;
      	 	Log.d(Tag, "Angle added" + coord.get(coord.size()-1).xA);
       	 	Log.d(Tag, "coordinate added " + coord.get(coord.size()-1).xC);
       	 	Log.d(Tag, " " + distance(coord.get(ind))); 
		}
	}
}